Imports Pacientes
Imports System.Data.SqlClient
Public Class TurnosCORREGIDO
 
    Private TipoDoc As TipoDocumento = Nothing
    Private paci As Paciente = Nothing
    Private tur As Turnos = Nothing
    Private prof As Profesional = Nothing
    Private id As Integer = 0
    Private numDoc As String = ""
    Private profes As String = ""

    Private idH As Integer = 0
    Private mat As Integer = 0
    '******************
    Private objTurno As Turnos
    Private turAsig As TurnosxPaciente
    Private FecTurno As Date
    Private Profesional As String
    Private objProfesional As Profesional
    Private nrotur As Integer = 0

    Public Property ProfTur() As String
        Get
            Return Profesional
        End Get
        Set(ByVal value As String)
            Profesional = value
        End Set
    End Property
    Public Property FechaTurnos() As Date
        Get
            Return FecTurno
        End Get
        Set(ByVal value As Date)
            FecTurno = value
        End Set
    End Property
    Public Property TurnosPac() As Turnos
        Get
            Return objTurno
        End Get
        Set(ByVal value As Turnos)
            objTurno = value
        End Set
    End Property
    Public Property Paciente() As Paciente
        Get
            Return paci
        End Get
        Set(ByVal value As Paciente)
            paci = value
        End Set
    End Property
    Public Property TurAsignados() As TurnosxPaciente
        Get
            Return turAsig
        End Get
        Set(ByVal value As TurnosxPaciente)
            turAsig = value
        End Set
    End Property
    Public Property profesionalObj() As Profesional
        Get
            Return objProfesional
        End Get
        Set(ByVal value As Profesional)
            objProfesional = value
        End Set
    End Property

    Private Sub optNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optNuevo.Click
        txtNroTurno.Enabled = False
        optNroTurno.Enabled = False
        'lblNroTurno.Enabled = False
        dtpFecha.Enabled = False
        lblFecha.Enabled = False

        cmdBuscar.Enabled = True
        cmdVerAgenda.Enabled = True
        cmdEstudios.Enabled = True

        lblHoraTurno.Text = ""
        lblProfTurno.Text = ""
        txtNroTurno.Text = ""

    End Sub
    Private Sub cargarComboTipoDoc()

        Dim datos As DataSet = Nothing
        TipoDoc = New TipoDocumento()
        datos = TipoDoc.BuscarDatos(conexion)
        Me.cboTipo.DataSource = datos.Tables(0)
        Me.cboTipo.DisplayMember = "descrip"
        Me.cboTipo.ValueMember = "idtipodoc"

        Dim index As Integer = 0
        index = Me.cboTipo.FindString(Me.cboTipo.Text)
        Me.cboTipo.SelectedIndex = index

        cboTipo.Text = ""
    End Sub

    Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click

        Dim objPacBuscado As Paciente = New Paciente()
        Dim objPacBuscadoTur As TurnosxPaciente = New TurnosxPaciente()

        Dim ape As String = ""
        Dim nom As String = ""
        Dim nrotur As Integer = 0
        Dim fectur As Date = Nothing

        If txtNroTurno.Text <> "" Then
            nrotur = txtNroTurno.Text
        End If
        If txtApe.Text <> "" And txtNom.Text <> "" Then
            ape = txtApe.Text
            nom = txtNom.Text
        End If
        fectur = Format(dtpFecha.Value, "dd/MM/yyyy")

        '********Asignacion de parametros

        If txtNroTurno.Text = "" And txtApe.Text = "" And txtNom.Text = "" Then
            'a la tabla paciente y me devuelve ape y nom
            BuscarTurnoxTNdoc()
            BuscarTurnoAsignado()

            optModificar.Enabled = True
            optAnular.Enabled = True
            optNuevo.Enabled = True

        Else
            optNuevo.Enabled = True

        End If

        If txtNroTurno.Text = "" And cboTipo.Text = "" And txtNroDoc.Text = "" Then
            If objPacBuscado.BuscarAN(ape, nom, conexion) Then
                id = objPacBuscado.tipodocumentoPer
                If id = 1 Then
                    cboTipo.Text = "DNI"
                End If
                If id = 2 Then
                    cboTipo.Text = "LC"
                End If
                If id = 3 Then
                    cboTipo.Text = "CI"
                End If
                If id = 4 Then
                    cboTipo.Text = "LE"
                End If
                If id = 5 Then
                    cboTipo.Text = "MAT.PROF"
                End If
                numDoc = objPacBuscado.NroDocumentoPer
                ban = 20
                BuscarTurnoAsignado()
                '*************************************************************
                If objPacBuscado.Buscar(id, numDoc, conexion) Then
                    txtTipoPac.Text = objPacBuscado.TipoPaciente.Descripcion
                    If txtTipoPac.Text = "Afiliado" Then
                        'DATOS DE OBRA SOCIAL
                        txtNomOS.Text = objPacBuscado.ObraSocialP.DescripOS
                        txtPlan.Text = objPacBuscado.NomPlanPac
                        txtNroAfiliado.Text = objPacBuscado.NroAfiliadoP
                    Else
                        fraObraSocial.Enabled = False
                    End If
                    'combo telefono de paciente
                    Dim objPacTel As TelefonoXPersona = New TelefonoXPersona()

                    Dim datos As DataSet = Nothing
                    datos = objPacTel.BuscarDatos(id, numDoc, conexion)
                    cboNroTel.DataSource = datos.Tables(0)
                    cboNroTel.DisplayMember = "nrotel"
                    cboNroTel.ValueMember = "idtipotel"
                End If
                '***********************************************************************
                optModificar.Enabled = True
                optAnular.Enabled = True
                optNuevo.Enabled = True

            Else
                optNuevo.Enabled = True
            End If
        End If
        'busca por nro de turno
        If cboTipo.Text = "" And txtNroDoc.Text = "" And txtApe.Text = "" And txtNom.Text = "" Then
            If objPacBuscadoTur.BuscarNroTurno(nrotur, fectur, conexion) Then
                If objPacBuscadoTur.fechaTP < Format(Now, "dd/MM/yyyy") Then
                    EliminarTurno()
                Else
                    Dim tur As Turnos = Nothing
                    tur = New Turnos()
                    idH = objPacBuscadoTur.idhoraTP
                    With Me.tur
                        If tur.Buscar(idH, conexion) Then
                            lblHoraTurno.Text = tur.HoraT
                        End If
                    End With
                    Dim h As Long
                    Dim hs As Long
                    Dim m As Long
                    Dim ms As Long
                    h = Hour(tur.HoraT)
                    hs = Hour(Now)
                    If hs = 0 Then
                        hs = 24
                    End If
                    m = Minute(tur.HoraT)
                    ms = Minute(Now)
                    If h < hs Then  'hs turno menor a hs sistema
                        EliminarTurno()
                    Else
                        If h > hs Then 'hs turno menor a hs sistema
                            'turno pendiente
                            lblMensaje.Visible = True
                            lblMensaje.Text = "EL PACIENTE TIENE TURNO PENDIENTE"

                            optPen.Checked = True

                            id = objPacBuscadoTur.tipodocTP
                            numDoc = objPacBuscadoTur.nrodocTP
                            If id = 1 Then
                                cboTipo.Text = "DNI"
                            End If
                            If id = 2 Then
                                cboTipo.Text = "LC"
                            End If
                            If id = 3 Then
                                cboTipo.Text = "CI"
                            End If
                            If id = 4 Then
                                cboTipo.Text = "LE"
                            End If
                            If id = 5 Then
                                cboTipo.Text = "MAT.PROF"
                            End If
                            txtNroDoc.Text = numDoc

                            Dim prof As Profesional = Nothing
                            prof = New Profesional()
                            mat = objPacBuscadoTur.matri
                            With Me.tur
                                If prof.Buscar(mat, conexion) Then
                                    lblProfTurno.Text = prof.apenomProf
                                End If
                            End With

                            ban = 20
                            BuscarTurnoxTNdoc()

                            optModificar.Enabled = True
                            optAnular.Enabled = True
                            optNuevo.Enabled = True
                        Else
                            If m <= ms Then 'min turno /min sistema
                                lblMensaje.Visible = True
                                lblMensaje.Text = "EL PACIENTE TIENE TURNO PENDIENTE"

                                optPen.Checked = True

                                id = objPacBuscadoTur.tipodocTP
                                numDoc = objPacBuscadoTur.nrodocTP
                                If id = 1 Then
                                    cboTipo.Text = "DNI"
                                End If
                                If id = 2 Then
                                    cboTipo.Text = "LC"
                                End If
                                If id = 3 Then
                                    cboTipo.Text = "CI"
                                End If
                                If id = 4 Then
                                    cboTipo.Text = "LE"
                                End If
                                If id = 5 Then
                                    cboTipo.Text = "MAT.PROF"
                                End If
                                txtNroDoc.Text = numDoc

                                Dim prof As Profesional = Nothing
                                prof = New Profesional()
                                mat = objPacBuscadoTur.matri
                                With Me.tur
                                    If prof.Buscar(mat, conexion) Then
                                        lblProfTurno.Text = prof.apenomProf
                                    End If
                                End With

                                ban = 20
                                BuscarTurnoxTNdoc()

                                optModificar.Enabled = True
                                optAnular.Enabled = True
                                optNuevo.Enabled = True
                            Else
                                EliminarTurno()
                            End If
                        End If
                    End If
                End If
            Else
                    optNuevo.Enabled = True
                    MsgBox("NRO.DE TURNO INEXISTENTE", MsgBoxStyle.Information, "LABS")
                txtNroTurno.Text = ""
                Cancelar()
                Exit Sub
            End If
        End If
        cmdBuscar.Enabled = False

        If lblHoraTurno.Text = "" Then
            optNuevo.Enabled = True
        End If
        If txtApe.Text = "" Or txtNom.Text = "" Or cboTipo.Text = "" Or txtNroDoc.Text = "" Then
            If MsgBox("El Paciente no Existe, DESEA REGISTRAR UN NUEVO PACIENTE?", vbYesNo + vbQuestion) = vbYes Then
                bande = 50
                Dim frm As frmActDatosPacienteCORREGIDO = New frmActDatosPacienteCORREGIDO()
                frm.ShowDialog(Me)
                frm.Dispose()
            Else
                MsgBox("NO PUEDE REALIZAR NINGUNA GESTION.", MsgBoxStyle.Information, "Atencion!")
                Cancelar()
                Exit Sub
            End If
        End If
    End Sub
    
    Private Sub BuscarTurnoxTNdoc()
        Dim objPacBuscado As Paciente = New Paciente()

        If cboTipo.Text = "DNI" Then
            id = 1
        End If
        If cboTipo.Text = "LC" Then
            id = 2
        End If
        If cboTipo.Text = "CI" Then
            id = 3
        End If
        If cboTipo.Text = "LE" Then
            id = 4
        End If
        If cboTipo.Text = "MAT.PROF" Then
            id = 5
        End If

        If ban = 20 Then
            numDoc = numDoc
        Else
            numDoc = txtNroDoc.Text()
        End If

        If objPacBuscado.Buscar(id, numDoc, conexion) Then
            txtApe.Text = objPacBuscado.ApellidoPer
            txtNom.Text = objPacBuscado.NombrePer
            txtTipoPac.Text = objPacBuscado.TipoPaciente.Descripcion
            If txtTipoPac.Text = "Afiliado" Then
                'DATOS DE OBRA SOCIAL
                txtNomOS.Text = objPacBuscado.ObraSocialP.DescripOS
                txtPlan.Text = objPacBuscado.NomPlanPac
                txtNroAfiliado.Text = objPacBuscado.NroAfiliadoP
            Else
                fraObraSocial.Enabled = False
            End If
            'combo telefono de paciente
            Dim objPacTel As TelefonoXPersona = New TelefonoXPersona()

            Dim datos As DataSet = Nothing
            datos = objPacTel.BuscarDatos(id, numDoc, conexion)
            cboNroTel.DataSource = datos.Tables(0)
            cboNroTel.DisplayMember = "nrotel"
            cboNroTel.ValueMember = "idtipotel"


        End If

    End Sub
    Private Sub BuscarTurnoAsignado()
        Dim objPacBuscadoTur As TurnosxPaciente = New TurnosxPaciente()
        Dim objPacBuscado As Paciente = New Paciente()


        If cboTipo.Text = "DNI" Then
            id = 1
        End If
        If cboTipo.Text = "LC" Then
            id = 2
        End If
        If cboTipo.Text = "CI" Then
            id = 3
        End If
        If cboTipo.Text = "LE" Then
            id = 4
        End If
        If cboTipo.Text = "MAT.PROF" Then
            id = 5
        End If
        If ban = 20 Then
            numDoc = numDoc
        Else
            numDoc = txtNroDoc.Text()
        End If

        If objPacBuscadoTur.BuscarTurnoTNdoc(id, numDoc, conexion) Then
            If objPacBuscadoTur.fechaTP < Format(Now, "dd/MM/yyyy") Then
                nrotur = objPacBuscadoTur.NroTur
                EliminarTurno()
            Else
                Dim tur As Turnos = Nothing
                tur = New Turnos()
                idH = objPacBuscadoTur.idhoraTP
                With Me.tur
                    If tur.Buscar(idH, conexion) Then
                        lblHoraTurno.Text = tur.HoraT
                    End If
                End With
                Dim h As Long
                Dim hs As Long
                Dim m As Long
                Dim ms As Long
                h = Hour(tur.HoraT)
                hs = Hour(Now)
                If hs = 0 Then
                    hs = 24
                End If
                m = Minute(tur.HoraT)
                ms = Minute(Now)
                If h < hs Then  'hs turno menor a hs sistema
                    '   EliminarTurno()
                    'Else
                    'If h >= hs Then 'hs turno menor a hs sistema
                    ' If m <= ms Then 'min turno /min sistema
                    lblMensaje.Visible = True
                    lblMensaje.Text = "EL PACIENTE TIENE TURNO PENDIENTE"

                    optPen.Checked = True

                    'FUNCION PARA QUE DEVUELVA LA DESC DEL TIPO DOC
                    txtNroDoc.Text = numDoc
                    txtNroTurno.Text = objPacBuscadoTur.NroTur
                    dtpFecha.Value = objPacBuscadoTur.fechaTP
                    dtpFechaTur.Value = objPacBuscadoTur.fechaTP

                    'DATOS PROFESIONAL
                    Dim prof As Profesional = Nothing
                    prof = New Profesional()
                    mat = objPacBuscadoTur.matri
                    With Me.tur
                        If prof.Buscar(mat, conexion) Then
                            lblProfTurno.Text = prof.apenomProf
                        End If
                    End With
                    ' End If
                    'Else
                    'EliminarTurno()
                    'End If
            End If
            End If
        End If
    End Sub

    Private Sub cmdVerAgenda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVerAgenda.Click
        Dim frm As frmTurnos = New frmTurnos()
        frm.ShowDialog(Me)
        frm.Dispose()
        With Me.TurnosPac
            dtpFechaTur.Value = CStr(Format(Me.FechaTurnos, "dd/MM/yyyy"))
            lblHoraTurno.Text = .HoraT
            lblProfTurno.Text = Me.ProfTur
            optPen.Checked = True
        End With
    End Sub
    Private Sub TurnosCORREGIDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarComboTipoDoc()
    End Sub
    Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click
        Close()
    End Sub
    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        LimpiarControles()
        cmdBuscar.Enabled = True
        optNuevo.Enabled = False
        optModificar.Enabled = False
        optAnular.Enabled = False
        cmdVerAgenda.Enabled = False
        cmdEstudios.Enabled = False
        cargarComboTipoDoc()

        optModificar.Checked = False
        optAnular.Checked = False
        optNuevo.Checked = False
        optPen.Checked = False
        optEnCurso.Checked = False
        txtNroTurno.Enabled = False
        optNroTurno.Checked = False
        optNroTurno.Enabled = True
        dtpFecha.Enabled = True


    End Sub
    Private Sub Cancelar()
        LimpiarControles()
        cmdBuscar.Enabled = True
        optNuevo.Enabled = False
        optModificar.Enabled = False
        optAnular.Enabled = False
        cmdVerAgenda.Enabled = False
        cmdEstudios.Enabled = False
        cargarComboTipoDoc()

        optModificar.Checked = False
        optAnular.Checked = False
        optNuevo.Checked = False
        optPen.Checked = False
        optEnCurso.Checked = False
        txtNroTurno.Enabled = False
        optNroTurno.Checked = False
        optNroTurno.Enabled = True
        dtpFecha.Enabled = True

    End Sub
    Private Sub LimpiarControles()
        optNuevo.Checked = False
        txtNroTurno.Text = ""
        cboTipo.Text = ""
        txtNroDoc.Text = ""
        txtApe.Text = ""
        txtNom.Text = ""
        lblProfTurno.Text = ""
        lblHoraTurno.Text = ""
        lblMensaje.Visible = False
        txtObserv.Text = ""
        txtTipoPac.Text = ""

        cboNroTel.DataSource = Nothing
        txtNomOS.Text = ""
        txtPlan.Text = ""
        txtNroAfiliado.Text = ""
    End Sub
    Private Sub optModificar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optModificar.Click
        cmdVerAgenda.Enabled = True
        optAnular.Enabled = False
        cmdEstudios.Enabled = True
       
        Dim mat1 As Integer = 0
        Dim id1 As Integer = 0
        Dim doc1 As String = ""
        Dim idh1 As Integer = 0
        Dim fec1 As Date = Nothing
        Dim numTur1 As Integer = 0
        id1 = id
        doc1 = txtNroDoc.Text
        idh1 = idH
        fec1 = Format(dtpFechaTur.Value, "dd/MM/yyyy")
        mat1 = mat
        numTur1 = txtNroTurno.Text
        Dim objEliminar As TurnosxPaciente = Nothing
        objEliminar = New TurnosxPaciente()
        With Me.tur
            objEliminar.EliminarTur(id1, doc1, numTur1, fec1, mat1, conexion)
        End With
    End Sub
    Private Sub cmdEstudios_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEstudios.Click
        banCalMon = 1
        Dim frm As frmCalcularMontos = New frmCalcularMontos()
        frm.ShowDialog(Me)
        frm.Dispose()
    End Sub
    Private Sub EliminarTurno()
        If MsgBox("Esta seguro que desea ANULAR EL TURNO? ", vbYesNo + vbQuestion) = vbYes Then
            Dim objEliminar As TurnosxPaciente = Nothing
            objEliminar = New TurnosxPaciente()

            'DATOS PROFESIONAL
            Dim prof As Profesional = Nothing
            prof = New Profesional()
            mat = objEliminar.matri
            With Me.tur
                If prof.Buscar(mat, conexion) Then
                    lblProfTurno.Text = prof.apenomProf
                End If
            End With
            'Dim nrotur As Integer = 0
            Dim fectur As Date = Nothing


            If cboTipo.Text = "DNI" Then
                id = 1
            End If
            If cboTipo.Text = "LC" Then
                id = 2
            End If
            If cboTipo.Text = "CI" Then
                id = 3
            End If
            If cboTipo.Text = "LE" Then
                id = 4
            End If
            If cboTipo.Text = "MAT.PROF" Then
                id = 5
            End If
            numDoc = txtNroDoc.Text
            fectur = Format(dtpFechaTur.Value, "dd/MM/yyyy")
            ' nrotur = txtNroTurno.Text
            With Me.tur
                objEliminar.EliminarTur(id, numDoc, nrotur, fectur, mat, conexion)

                LimpiarControles()
                cmdBuscar.Enabled = True
                optNuevo.Enabled = False
                optModificar.Enabled = False
                optAnular.Enabled = False
                cmdVerAgenda.Enabled = False
                cmdEstudios.Enabled = False
                cargarComboTipoDoc()
                optModificar.Checked = False
                optAnular.Checked = False
                optNuevo.Checked = False
            End With
        End If

    End Sub
    Private Sub optAnular_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optAnular.Click
        EliminarTurno()
    End Sub

    Private Sub cmdAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAceptar.Click
        Dim objTurnoAsignado As TurnosxPaciente = New TurnosxPaciente()
        objTurnoAsignado.idhoraTP = objTurno.IdHoraT
        objTurnoAsignado.fechaTP = Format(Me.dtpFechaTur.Value, "dd/MM/yyyy")

        If cboTipo.Text = "DNI" Then
            id = 1
        End If
        If cboTipo.Text = "LC" Then
            id = 2
        End If
        If cboTipo.Text = "CI" Then
            id = 3
        End If
        If cboTipo.Text = "LE" Then
            id = 4
        End If
        If cboTipo.Text = "MAT.PROF" Then
            id = 5
        End If
        numDoc = txtNroDoc.Text


        objTurnoAsignado.tipodocTP = id 'Paciente.tipodocumentoPer
        objTurnoAsignado.nrodocTP = numDoc ' Paciente.NroDocumentoPer
        objTurnoAsignado.estado = 6
        'DATOS PROFESIONAL
        Dim prof As Profesional = Nothing
        prof = New Profesional()
        profes = lblProfTurno.Text 'txtProfTurno.Text
        With Me.prof
            If prof.BuscarProf(profes, conexion) Then
                objTurnoAsignado.matri = prof.MatriculaProf
            End If
        End With
        objTurnoAsignado.NroTur = objTurno.IdHoraT
        objTurnoAsignado.contac = cboNroTel.Text
        'Busco PK duplicada
        If objTurnoAsignado.validarPK(objTurnoAsignado.idhoraTP, objTurnoAsignado.fechaTP, objTurnoAsignado.tipodocTP, objTurnoAsignado.nrodocTP, objTurnoAsignado.matri, objTurnoAsignado.NroTur, conexion) > 0 Then
            MsgBox("El turno ya ha sido asignado", MsgBoxStyle.Information, "Atencion!")
            Exit Sub
        End If

        objTurnoAsignado.Agregar(conexion)
        LimpiarControles()
        txtNroTurno.Enabled = True
        optNroTurno.Enabled = False
        'lblNroTurno.Enabled = True
        optNuevo.Enabled = False
        optModificar.Enabled = False
        optAnular.Enabled = False
        cmdVerAgenda.Enabled = False
        cmdEstudios.Enabled = False
        optNroTurno.Checked = False
        optNroTurno.Enabled = True
        dtpFecha.Enabled = True

    End Sub
    Function SoloNumeros(ByVal Keyascii As Short) As Short
        If InStr("1234567890", Chr(Keyascii)) = 0 Then
            SoloNumeros = 0
        Else
            SoloNumeros = Keyascii
        End If
        Select Case Keyascii
            Case 8
                SoloNumeros = Keyascii
            Case 13
                SoloNumeros = Keyascii
        End Select
    End Function
    Private Sub txtNroTurno_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroTurno.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(SoloNumeros(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtNroDoc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroDoc.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(SoloNumeros(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub

    Private Sub optNroTurno_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optNroTurno.Click
        optNroTurno.Checked = True
        txtNroTurno.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("El Comprobante de Turno fue Emitido")
    End Sub


End Class